const app = getApp();
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isHide: false,
    button: false,
    xbtp: true,
    swiperList: [],
    userInfo: [],
    xb:''
  },

  onLoad: function () {
    this.getOpenid()
    this._getggao()
  },

  getList: function () {
    db.collection('zl').where({
      _id: this.data.openid,
      _openid: this.data.openid,
    }).get().then((res) => {
      this.setData({
        zlList: res.data[0]
      })
    }).catch(err => {
      // 查询数据失败
      console.log(err)
    })
  },
  _getggao() {
    wx.cloud.callFunction({
      name: 'ggao',
      data: {
        start: 0,
        count: 3,
        $url: 'lost',
      }
    }).then((res) => {
      // console.log(res)
      this.setData({
        gglist: res.result
      })
      wx.hideLoading()

    })
  },
  xbtp() {
    if (this.data.zlList.xb == 666||this.data.zlList.xb == 999) {
      this.setData({
        xbtp: false
      })
    }
  },
  xbgl() {
    wx.navigateTo({
      url: '/pages/profile/heji/xbgl/xbgl?openid=' + this.data.zlList._openid,
    })
  },
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
      that.setData({
        userInfo: e.detail.userInfo
      });
      console.log(this.data.openid)
      if (this.data.openid == '') {
        wx.showModal({
          title: '网络错误，请刷新',
        })
        return
      }
      var userInfo = e.detail.userInfo
      let promiseArr = []
      Promise.all(promiseArr).then((res) => {
        db.collection('zl').add({
          data: {
            ...userInfo,
            _id:this.data.openid,
            xb:1,
            createTime: db.serverDate(),//获取服务端的时间
          }
        }).then((res) => {
          wx.showToast({
            title: '登录成功',
          })
          that.setData({
            button:true
          })
          that.xbtp()
      }).catch((err) => {
        wx.hideLoading()
        wx.showToast({
          title: '您已登录',
        })
        this.setData({
          button:true
        })
        
        that.xbtp()
      })
    })
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (res.confirm) {
            console.log('用户点击了“返回授权”');
          }
        }
      });
    }
  },

  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        var openid = res.result.openid;
        that.setData({
          openid: openid
        })
        this.getList()
      }
    })
  },
  lx: function () {

    wx.showModal({
      title: '联系方式：123145678911',
    })
  },
  wdzj: function (event) {
    wx.navigateTo({
      url: "/pages/profile/wdzj/wdzj",
    })
  },
  wdpl: function (event) {
    wx.navigateTo({
      url: "/pages/profile/wdpl/wdpl",
    })
  },
  wdzl: function (event) {
    wx.navigateTo({
      url: `/pages/profile/heji/zl/zl`,
    })
  },

  wdfb: function (event) {
    wx.navigateTo({
      url: "/pages/profile/fabu/fabu",
    })
  },

  wddz: function (event) {
    wx.navigateTo({
      url: "/pages/profile/wdsc/wdsc",
    })
  },
  wdxx: function (event) {
    wx.navigateTo({
      url: "/pages/profile/heji/xx/xx",
    })
  },
  rz: function (event) {
    wx.navigateTo({
      url: "/pages/profile/heji/rz/rz",
    })
  },
  wdnl: function (event) {
    wx.navigateTo({
      url: "/pages/profile/wdnl/wdnl",
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})